package cn.xshi.sys.web;

import cn.xshi.sys.model.SysIcon;
import cn.xshi.sys.form.SysIconForm;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.sys.service.SysIconService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 字体图标库
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/icon")
@Api(value = "字体图标库API",tags = "字体图标库API",description = "字体图标库API")
public class SysIconController extends BaseAction{

    @Resource
    SysIconService sysIconService;

    /**
     * 加载初始化列表数据并分页
     * @param sysIconForm
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value="查询字体库图标列表并分页", notes="查询字体库图标列表并分页")
    @GetMapping(value="/list")
    @Auth(value = "/icon/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<SysIcon>> getSysIconListByCondition(SysIconForm sysIconForm,Integer pageNo, Integer pageSize){
        commonHPagerByPageNo(pageNo,pageSize);
        List<SysIcon> sysIconList = sysIconService.getSysIconListByCondition(sysIconForm);
        PageInfo<SysIcon> page = new PageInfo<SysIcon>(sysIconList);
        return outPageBootByPageNo(page,pageNo,pageSize);
    }

    /**
     * 查询单个字体库
     * @param id
     */
    @ApiOperation(value="查询单个字体库", notes="查询单个字体库")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/icon/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<SysIcon> getSysIconById(@PathVariable("id")String id){
        SysIcon sysIcon = sysIconService.getSysIconById(id);
        if(!StringUtil.isEmpty(sysIcon.getCreateId())){
            OauthAccountEntity createBy = getAccount(sysIcon.getCreateId());
            if(null != createBy){
                sysIcon.setCreateBy(createBy.getName());
            }
        }
        if(!StringUtil.isEmpty(sysIcon.getUpdateId())){
            OauthAccountEntity modifiedBy = getAccount(sysIcon.getUpdateId());
            if(null != modifiedBy){
                sysIcon.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(sysIcon);
    }

    /**
     * 添加
     * @param sysIcon
     */
    @PostMapping(value="/add")
    @ApiOperation(value="创建单个字体库图标", notes="创建单个字体库图标")
    public BaseResult addSysIcon(@RequestBody SysIcon sysIcon){
        int i = 0;
        if(null != sysIcon){
            sysIcon.setId(toUUID());
            sysIcon.setCreateTime(getDate());
            sysIcon.setCreateId(getCurrentAccountId());
            i= sysIconService.addSysIcon(sysIcon);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param sysIcon
     */
    @PutMapping(value="/update")
    @ApiOperation(value="编辑单个字体库图标", notes="编辑单个字体库图标")
    public BaseResult updateSysIcon(@RequestBody SysIcon sysIcon){
        int i = 0;
        if(null != sysIcon){
            sysIcon.setUpdateTime(getDate());
            sysIcon.setUpdateId(getCurrentAccountId());
            i= sysIconService.updateSysIcon(sysIcon);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value="/delete")
    @ApiOperation(value="删除字体库图标", notes="删除字体库图标")
    public BaseResult delSysIcon(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i= sysIconService.delSysIcon(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 查询字体库图标列表
     * @param sysIconForm
     */
    @ApiOperation(value="查询字体库图标列表", notes="查询字体库图标列表")
    @GetMapping(value="/query/list")
    @Auth(value = "/icon/query/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<SysIcon>> getSysIconList(SysIconForm sysIconForm){
        List<SysIcon> sysIconList = sysIconService.getSysIconList(sysIconForm);
        return BaseResult.success(sysIconList);
    }

//    @PostMapping(value="/batchAdd")
//    @AuthUneedLogin
//    public BaseResult batchAdd(String msg){
//        String[]array = msg.split(",");
//        for(String str:array){
//            SysIcon SysIcon = new SysIcon();
//            str = str.trim();
//            if(str.substring(0,1).equals("l")){
//                continue;
//            }
//            SysIcon.setName(str);
//            SysIcon.setIcon(str);
//            SysIcon.setId(toUUID());
//            SysIcon.setCreateId(getCurrentAccountId());
//            SysIcon.setCreateTime(getDate());
//            SysIcon.setCategories("Lineawesome");
//            SysIconService.addSysIcon(SysIcon);
//        }
//        return BaseResult.success();
//    }
}
